define(["jquery", "easy-admin","xm-select"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'buddha.article_index/index',
        add_url: 'buddha.article_index/add',
        edit_url: 'buddha.article_index/edit',
        delete_url: 'buddha.article_index/delete',
        export_url: 'buddha.article_index/export',
        modify_url: 'buddha.article_index/modify',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'id'},
                    {field: 'image', title: '图片', templet: ea.table.image,search:'false'},
                    {field: 'getArticle', title: '连接文章'},
                    {field: 'status', search: 'select', selectList: ["禁用","启用"], title: '状态', templet: ea.table.switch},
                    {field: 'create_time', title: '创建时间',search: 'range'},
                    {width: 250, title: '操作', templet: ea.table.tool},
                ]],
            });

            ea.listen();
        },
        add: function () {
            xmSelect.render({
                el: '#article_index',
                autoRow: true,
                toolbar: { show: true },
                filterable: true,
                remoteSearch: true,
                radio:true,
                clickClose:true,
                name:'article_id',
                remoteMethod: function(val, cb, show){
                    //这里如果val为空, 则不触发搜索
                    if(!val){
                        return cb([]);
                    }
                    // todo cb(res.data)
                    let province = $("#province").val();
                    $.post("/admin/buddha.article/getArticleByIdList",{
                        val:val
                    },function (res) {
                        if(res.code == 1){
                            return cb(res.data);
                        }else{
                            layer.msg(res.msg,{icon:5,shade:0.1})
                        }
                    })
                },
                on:function (data) {
                    let arr = data.arr;
                    if(arr.length != 0){
                        $("#lat").val(arr[0].lat);
                        $("#lng").val(arr[0].lng);
                    }
                }
            })
            ea.listen();
        },
        edit: function () {
            xmSelect.render({
                el: '#article_index',
                autoRow: true,
                toolbar: { show: true },
                filterable: true,
                remoteSearch: true,
                radio:true,
                clickClose:true,
                name:'article_id',
                remoteMethod: function(val, cb, show){
                    //这里如果val为空, 则不触发搜索
                    if(!val){
                        return cb([]);
                    }
                    // todo cb(res.data)
                    let province = $("#province").val();
                    $.post("/admin/buddha.article/getArticleByIdList",{
                        val:val
                    },function (res) {
                        if(res.code == 1){
                            return cb(res.data);
                        }else{
                            layer.msg(res.msg,{icon:5,shade:0.1})
                        }
                    })
                },
                on:function (data) {
                    let arr = data.arr;
                    if(arr.length != 0){
                        $("#lat").val(arr[0].lat);
                        $("#lng").val(arr[0].lng);
                    }
                }
            })
            ea.listen();
        },
    };
    return Controller;
});